package com.angel.controller;

import com.angel.bean.BabyInfo;
import com.angel.bean.Employee;
import com.angel.bean.IllCase;
import com.angel.bean.dto.BabyInfoDto;
import com.angel.exception.StatusException;
import com.angel.service.IllCaseService;
import com.angel.service.OtherlogService;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.LogUtils;
import com.github.pagehelper.PageInfo;
import com.angel.service.BabyInfoService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/babyInfoController")
public class BabyInfoController {

      @Resource
      private BabyInfoService babyInfoService;
    @Autowired
    private OtherlogService otherlogService;

    @Autowired
    private IllCaseService illCaseService;



    /**
     * 添加宝宝信息
     * @return
     */
    @RequestMapping("/addBabyInfo")
    @ResponseBody
    public Object addBabyInfo(BabyInfo babyInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        babyInfo.setState(1);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        int result = babyInfoService.insert(babyInfo);
        if (result != 0) {
            msg = "添加宝宝："+babyInfo.getBabyName()+"信息成功";
            map.put("result", "success");
        }else {
            msg = "添加宝宝："+babyInfo.getBabyName()+"失败";
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
     }

    /**
     *跳转到添加宝宝页面
     * @param
     * @param
     * @return
     */
    @RequestMapping("/addBaby")
    public String addBaby() {
        return "babyInfoEntryAdd";
    }


    /**
     * 跳转到查看所有宝宝页面
     * 分页查询
     * @param model
     * @param babyInfo
     * @return
     */
    @RequestMapping("/findBaby")
    @ExceptionHandler(value = StatusException.class)
    public String findBaby(@RequestParam(value="pn",defaultValue="1")String  pn, Model model, BabyInfo babyInfo) {
        //将前端传回的参数改为String类型，然后进行数据类型转换，如果出现问题，则进行异常处理，异常可以是空处理
        int pageNo =1;
        try {
            pageNo =Integer.valueOf(pn);
        }catch (Exception e){

        }

        PageHelper.startPage(pageNo, 5);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        babyInfo.setState(1);
        if (employee.getEmTypeId() > 2){
            babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        }
        babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        List<BabyInfoDto> babyInfoList = babyInfoService.findAllAndUser(babyInfo);
        PageInfo pageInfo = new PageInfo(babyInfoList,5);

        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "babyInfoEntry";
    }

    /**
     * 跳转到查看所有宝宝页面
     * 分页查询
     * @param model
     * @param babyInfo
     * @return
     */
    @RequestMapping("/seeBaby")
    @ExceptionHandler(value = StatusException.class)
    public String seeBaby(@RequestParam(value="pn",defaultValue="1")String  pn, Model model, BabyInfo babyInfo) {
        //将前端传回的参数改为String类型，然后进行数据类型转换，如果出现问题，则进行异常处理，异常可以是空处理
        int pageNo =1;
        try {
            pageNo =Integer.valueOf(pn);
        }catch (Exception e){

        }

        PageHelper.startPage(pageNo, 5);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        babyInfo.setState(1);
        if (employee.getEmTypeId() > 2){
            babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        }
        babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        List<BabyInfo> babyInfoList = babyInfoService.findAll(babyInfo);
        PageInfo pageInfo = new PageInfo(babyInfoList,5);

        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }

        return "checkBaby";
    }

    /**
     * 物理删除
    * @RequestMapping("/delBabyInfo")
    *@ResponseBody
     **/
    @RequestMapping("/delBabyInfo")
    @ResponseBody
    public Object delBabyInfo(BabyInfo babyInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        int result = babyInfoService.delete(babyInfo);
        if (result != 0) {
            msg ="删除宝宝信息(数据库存储)："+babyInfo.getBabyId()+"成功";
            map.put("result", "success");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }

    /**
     * 修改宝宝信息
     * @param babyInfo
     * @return
     */
    @RequestMapping("/updateBabyInfo")
    @ResponseBody
    public Object updateBabyInfo(BabyInfo babyInfo){
        Map<Object, Object> map = new HashMap<Object, Object>();
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        int result = babyInfoService.update(babyInfo);
        if (result != 0) {
            msg="更新宝宝信息(数据库存储信息)："+babyInfo.getBabyId();
            map.put("result", "success");
        }
        LogUtils.InsertLog(employee,otherlogService,msg);
        return map;
    }

    /**
     * 查询单个宝宝信息
     * @param babyInfo
     * @return
     */
    @RequestMapping("/findOneBabyInfo")
    @ResponseBody
    public Object findOneBabyInfo(BabyInfo babyInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        BabyInfo bb = babyInfoService.findOne(babyInfo);
        if (bb != null) {
            map.put("bb", bb);
        }
        return map;
    }


    @RequestMapping("/findOneBabyInfoAndstatus")
    @ResponseBody
    public Object findOneBabyInfoAndstatus(BabyInfo babyInfo) {
        Map<Object, Object> map = new HashMap<Object, Object>();
        BabyInfo bb = babyInfoService.findOne(babyInfo);
        IllCase illCase = new IllCase();
        illCase.setBabyId(babyInfo.getBabyId());
        illCase.setIllCaseState(6);
        int resultCount = illCaseService.getCountAndBabyid(illCase);
        if (bb != null) {
            map.put("bb", bb);
            map.put("serviceCount",resultCount);
        }
        return map;
    }



    @RequestMapping("/findBabyInfo")
    @ExceptionHandler(value = StatusException.class)
    public String findBabyInfo(@RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);
        BabyInfo babyInfo = new BabyInfo();
        babyInfo.setState(1);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        if (employee.getEmTypeId() > 2){
            babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        }
        List<BabyInfo> babyInfoList =  babyInfoService.findAll(babyInfo);
        PageInfo pageInfo = new PageInfo(babyInfoList,5);

        if (pageInfo != null) {
            model.addAttribute("pageInfo", pageInfo);
        }
        return "babyInfoEntry";
    }


    /**
     * 模糊查询
     * @param pn
     * @param model
     * @return
     */
    @RequestMapping("/searchBabyInfo")
    @ExceptionHandler(value = StatusException.class)
    public String searchBabyInfo(@RequestParam(value = "pn", defaultValue = "1") String pn,
                                 @RequestParam(value = "type") String type,
                                 @RequestParam(value = "content") String content,
                                 Model model) {
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {

        }
        content = content.trim().replaceAll(" ", "");
        PageHelper.startPage(pageNo, 5);
        BabyInfo babyInfo =  new BabyInfo();
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        if (employee.getEmTypeId() > 2){
            babyInfo.setReserve15(String.valueOf(employee.getBranchesId()));
        }
        List<BabyInfoDto> babyInfoList = null;
        switch (type) {
            case "fileNo":
                // 档案编号
                babyInfo.setFileNo(content);
                babyInfoList = babyInfoService.searchBabyInfo(babyInfo);
                break;
            case "babyName":
                // 宝宝姓名
                babyInfo.setBabyName(content);
                babyInfoList = babyInfoService.searchBabyInfo(babyInfo);
                break;
            default:
                break;
        }
        PageInfo pageInfo = new PageInfo(babyInfoList,5);

        model.addAttribute("type", type);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("content", content);
        return "babyInfoEntry2";
    }

}
